	

module.exports = {
	fotolaDatabase : null,
	connectDataBase : function() {
		var mongo = require('mongodb');
		var server = mongo.Server("localhost", 1105, {
			auto_reconnect : true
		});
		
		var FotolaDatabase = mongo.Db;

		fotolaDatabase = new FotolaDatabase('fotola', server, {
			safe : true
		});

		fotolaDatabase.open(function(error, client) {
			if (error) {
				return console.dir(error);
			} else {
				console.log("Connected to 'fotola' database");
			}

			client.authenticate('admin', 'admin', function(authError, success) {
				if (authError) {
					if(!fotolaDatabase){
						fotolaDatabase.close();
					}
					return console.dir(authError);
				}else{
					console.log("Authen successfully!");
				}
			});
		});
	},
	
	closeDatabase:function(){	
		if(!fotolaDatabase){
			fotolaDatabase.close();
		}
			
	}
	
}
